" --------------------------- 基础配置 ------------------------------{{{
" Vim模式
set nocompatible

" 配置目录
let $vimfiles = expand('<sfile>:p:h')
let $vimcache = $HOME . '/.vimcache'

" 引导键
let mapleader=','

" 文件编码
set encoding=utf-8
set termencoding=utf-8
set fileencoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1

" 持久撤消
set undofile
set undolevels=1000
set undodir=$vimcache/undofiles
if !isdirectory(&undodir)
    call mkdir(&undodir, 'p')
endif

" 状态保持
set viminfo='100,f1,<1000,s100,:100,@100,/100,n$vimcache/viminfo
set viewdir=$vimcache/viewfiles
auto BufWinLeave * if expand('%') != '' && &buftype == '' | mkview          | endif
auto BufReadPost * if expand('%') != '' && &buftype == '' | silent loadview | endif

" 备份配置
set backup
set noswapfile
auto BufReadPost * if getfsize(expand('%:p')) > 1048576 | set nobackup | endif

" 备份目录
let &backupdir=$vimcache.'/bakfiles/'.strftime('%y%m')
if !isdirectory(&backupdir)
    call mkdir(&backupdir, 'p')
endif

" 备份扩展名
let &backupext='_»_'.substitute(substitute(getcwd(), "['\" ():*?<>|]", "-", "g"), "[\\/]", "+", "g").'+'.strftime('%m%d%H').'.bak'
" }}}

" --------------------------- 插件配置 ------------------------------{{{
filetype off

" 插件管理
let $bundle = $vimfiles . '/bundle'
let $vundle = $vimfiles . '/bundle/vundle.vim'

set runtimepath+=$vundle
call vundle#begin($bundle)

" 插件管理
Plugin 'vundleVim/vundle.vim'

" 文件浏览
Plugin 'scrooloose/nerdtree'
let g:NERDTreeWinSize=42
let g:NERDTreeDirArrowExpandable='+'
let g:NERDTreeDirArrowCollapsible='~'
let g:NERDTreeIgnore=['^\.git$','^\.svn$','^\.idea']
auto bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
nnoremap <silent><c-t> :NERDTreeToggle<CR>

" 文件搜索
Plugin 'ctrlpvim/ctrlp.vim'
let g:ctrlp_map='<c-p>'
let g:ctrlp_cmd='CtrlP'
let g:ctrlp_by_filename=1
let g:ctrlp_regexp=1
let g:ctrlp_open_new_file='r'
let g:ctrlp_match_window='bottom,min:1,max:20'
let g:ctrlp_root_markers=['.svn','.git','.idea']
let g:ctrlp_cache_dir=$vimcache.'/ctrlp'
nnoremap <silent><c-e> :CtrlPBuffer<CR>

" 代码提示
Plugin 'Shougo/neocomplcache.vim'
let g:acp_enableAtStartup=0
let g:neocomplcache_enable_at_startup=1
let g:neocomplcache_enable_smart_case=1
let g:neocomplcache_enable_auto_select=1
let g:neocomplcache_temporary_dir=$vimcache.'/neocomplcache'
inoremap <expr><CR>   pumvisible() ? '<C-y>' : '<CR>'
inoremap <expr><C-CR> pumvisible() ? neocomplcache#cancel_popup().'<CR>' : '<CR>'

" 括号补全
Plugin 'Raimondi/delimitMate'
let delimitMate_quotes = "\" ' `"
let delimitMate_matchpairs="(:),[:]"
let delimitMate_nesting_quotes = ['"',"'"]

" 状态栏
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'
let g:airline_theme='solarized'
let g:airline_left_sep=''
let g:airline_right_sep=''
let g:airline_section_b='%{expand("%:p:h")}'

" 快速跳转
Plugin 'easymotion/vim-easymotion'
nmap <leader>f <Plug>(easymotion-s)
nmap <leader>F <Plug>(easymotion-s2)

" 缩进对齐
Plugin 'Yggdroot/indentLine'
let g:indentLine_enabled=1
let g:indentLine_fileType=['php', 'python']
let g:indentLine_char='¦'
let g:indentLine_color_term=239
let g:indentLine_color_gui='#4e4e4e'
nnoremap <silent><leader>id :IndentLinesToggle<CR>

" 快速注释
Plugin 'scrooloose/nerdcommenter'
let g:NERDSpaceDelims=1
let g:NERDDefaultAlign='left'
let g:NERDCommentEmptyLines=1
let g:NERDTrimTrailingWhitespace=1
let g:NERDCreateDefaultMappings=0
nmap <c-/> <Plug>NERDCommenterToggle
vmap <c-/> <Plug>NERDCommenterToggle

call vundle#end()
"}}}

" --------------------------- 界面配置 ------------------------------{{{
" 图形界面
if has('gui_running')
    " 外观配置
    set lines=35 columns=160
    set guioptions=gte
    set guitablabel=%N:%M%t

    " 标题配置
    auto BufEnter * let &titlestring = expand("%:p")

    " 消息编码
    set langmenu=zh_CN.UTF-8
    language message zh_CN.UTF-8

    " 鼠标配置
    set mouse=a
    set selection=inclusive
    set mousemodel=popup

    " 窗口调整
    nnoremap <silent><C-x> :simalt ~x<CR>
    nnoremap <silent><C-n> :simalt ~n<CR>

    " 复制粘贴
    vnoremap <C-c> "+y
    vnoremap <C-x> "+x
    nnoremap <C-a> ggVG
    exe 'inoremap <script><C-v>' paste#paste_cmd['i']

    " 字体配置
    set guifont=Monaco:h9:cANSI
endif

" 终端配置
if !has('gui_running')
    " 彩色终端
    set t_Co=256

    " 快速终端
    set ttyfast

    " tmux兼容
    if &term =~ '^screen'
        execute "set <xUp>=\e[1;*A"
        execute "set <xDown>=\e[1;*B"
        execute "set <xRight>=\e[1;*C"
        execute "set <xLeft>=\e[1;*D"
    endif
endif
" }}}

" --------------------------- 选项配置 ------------------------------{{{
" 语法高亮
syntax on

" 类型检测
filetype indent on
filetype plugin on

" 配色方案
set background=dark
colorscheme solarized

"  文件重载
set autoread
set autowrite

" 代码缩进
set cindent
set autoindent
set smartindent

" Tab配置
set smarttab
set expandtab
set shiftwidth=4
set softtabstop=4

" 显示配置
set list
set ruler
set number
set showcmd
set wildmenu
set laststatus=2
set shortmess=atI
set listchars=tab:^-,extends:»,precedes:«,trail:~

" 搜索配置
set magic
set incsearch
set hlsearch
set ignorecase
set smartcase

" 折行配置
set wrap
set textwidth=0
set backspace=2
set showbreak=»
set whichwrap=b,<,>,[,]
set foldmethod=marker
set formatoptions+=mM

" 消息提示
set hidden
set noerrorbells
set novisualbell
set t_vb=
set report=0

" 其它配置
set matchpairs=(:),[:],{:}
set tags=tags;
set complete+=k
set virtualedit=block
set completeopt=menuone,longest
set tabpagemax=20
" }}}

" --------------------------- 映射配置 ------------------------------{{{
"  :map         查看所有键绑定
"  :nmap <key>  查看普通模式键绑定

" 重载配置文件
nnoremap <leader>vi :runtime vimrc<cr>

" 重画屏幕
nnoremap <leader>rd :redraw!<cr>

" 保存/退出
nnoremap w :w<cr>
nnoremap q :q<cr>

" 复制操作
nnoremap <leader>yw "+yiw
nnoremap <leader>yy "+yy
nnoremap <leader>ya :%y+<cr>

" 插入模式
inoremap <c-h> <left>
inoremap <c-l> <right>
inoremap <c-a> <home>
inoremap <c-e> <end>
inoremap <c-d> <del>
inoremap <c-k> <up>
inoremap <c-j> <down>

" 命令模式
cnoremap <c-b> <left>
cnoremap <c-n> <down>
cnoremap <c-p> <up>
cnoremap <c-f> <right>
cnoremap <c-a> <home>
cnoremap <c-e> <end>
cnoremap <c-d> <del>

" 屏幕滚动
nnoremap <c-d> 5<c-d>
nnoremap <c-u> 5<c-u>
vnoremap <c-d> 5<c-d>
vnoremap <c-u> 5<c-u>
nnoremap <space> <c-f>

" 窗口移动
nnoremap <c-h> <c-w>h
nnoremap <c-j> <c-w>j
nnoremap <c-k> <c-w>k
nnoremap <c-l> <c-w>l

" 窗口调整
nnoremap <c-up>    <c-w>-
nnoremap <c-down>  <c-w>+
nnoremap <c-left>  <c-w><
nnoremap <c-right> <c-w>>

" 标记跳转
nnoremap <leader>t <c-]>
nnoremap <leader>T <c-t>

" 标签操作
nnoremap <silent>tn :tabnew<cr>
nnoremap <silent>tc :tabclose<cr>
nnoremap <silent><c-tab> :tabnext<cr>
nnoremap <silent><c-s-tab> :tabprevious<cr>

" 高亮单词
nnoremap <silent><leader>? :execute 'match search /\<'.expand('<cword>').'\>/'<cr>
nnoremap <silent><leader>/ :nohls<cr>:match none<cr>
" }}}

" --------------------------- 其它配置 ------------------------------{{{
" 清理文件
function! CleanFiles()
    silent retab
    silent set fileformat=unix
    silent %s/\r//e
    silent %s/\s\+$//e
endfunction
nnoremap <silent><leader>cf :call CleanFiles()<CR>

" 粘贴模式
function! TogglePaste()
    set paste!
    if exists('g:syntax_on') | syntax off | else | syntax enable | endif
endfunction
nnoremap <silent><leader>ps :call TogglePaste()<CR>

" 本地配置
if filereadable(glob('$vimcache/vimrc'))
    source $vimcache/vimrc
endif
" }}}
